#ifndef TRTRADERSPI_H
#define TRTRADERSPI_H
#include <thread>
#include "include/TrImpl.h"
#include "include/TrQuote.h"
#include "ThostFtdcTraderApi.h"


class CTrTraderSpi : public QObject, public CThostFtdcTraderSpi
{
    Q_OBJECT
public:
    CTrTraderSpi();
    ~CTrTraderSpi() override;
    CThostFtdcReqUserLoginField     m_FtdcReqUserLoginField;
    CThostFtdcTraderApi*            m_pFtdcTraderApi = nullptr;
    CTrImpl*                        m_pTrImpl = nullptr;
    CTrQuote*                       m_pThread = nullptr;
    int                             m_nResultID;
    int								m_nOrderRef;
    int                             m_nLastUpdatePositionTime;
    int                             m_nLastUpdateTradingAccount;
    int                             m_nLastInsertOrderTime;
    int                             m_nLastUserLoginSubmiTime;
    TThostFtdcTimeType              m_LoginTime;
    TThostFtdcBrokerIDType          m_BrokerID;
    TThostFtdcUserIDType            m_UserID;
    TThostFtdcAuthCodeType          m_AuthCode;
    TThostFtdcAppIDType             m_AppID;
    TThostFtdcFrontIDType           m_FrontID;
    TThostFtdcSessionIDType         m_SessionID;
    TThostFtdcOrderRefType          m_MaxOrderRef;
    CThostFtdcTradingAccountField   m_TradingAccount;
    QVector<CThostFtdcInvestorPositionField>    m_InvestorPosition;
    QVector<CThostFtdcOrderField>   m_FtdcOrder;
    QVector<CThostFtdcTradeField>   m_FtdcTrade;
    Tr_UserLoginStatus				m_LoginStatus;
    Tr_InvestorPositionStatus		m_InvestorPositionStatus;
    bool                            m_bIsQuotting;
public:
    bool                            CreatePositionBuy(TThostFtdcInstrumentIDType InstrumentID, int nLots, double dPrice, int nOffset = 0, QString source = "");
    bool                            CreatePositionSell(TThostFtdcInstrumentIDType InstrumentID, int nLots, double dPrice, int nOffset = 0, QString source = "");
    bool                            ClearPositionOnBuy(TThostFtdcInstrumentIDType InstrumentID, int nLots, double dPrice, int nOffset = 0, QString source = "");
    bool                            ClearPositionOnSell(TThostFtdcInstrumentIDType InstrumentID, int nLots, double dPrice, int nOffset = 0, QString source = "");
    void                            RevokeOrder(CThostFtdcOrderField *pOrder);
    void                            SetUserLoginField(char* szBrokerID, char* szUserID, char* szPassword);
    bool                            isErrorRspInfo(CThostFtdcRspInfoField *pRspInfo);
    bool                            bIsReadyTrade();
    void                            reqSettlementInfoConfirm();
    void                            reqRspQryInvestorPosition();
    void                            reqRspQryInstrument();
    void                            reqQueryTradingAccount();
    void                            reqOrderInsert(TThostFtdcInstrumentIDType InstrumentID, TThostFtdcOffsetFlagType CombOffsetFlag, TThostFtdcDirectionType Direction, TThostFtdcPriceType	LimitPrice, TThostFtdcVolumeType VolumeTotalOriginal, QString Source);
    int                             timeStamp();
public:
    virtual void OnFrontConnected() override;
    virtual void OnRspAuthenticate(CThostFtdcRspAuthenticateField *pRspAuthenticateField, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) override;
    virtual void OnFrontDisconnected(int nReason) override;
    virtual void OnRspUserLogin(CThostFtdcRspUserLoginField *pRspUserLogin, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) override;
    virtual void OnRspUserLogout(CThostFtdcUserLogoutField *pUserLogout, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) override;
    virtual void OnRspSettlementInfoConfirm(CThostFtdcSettlementInfoConfirmField *pSettlementInfoConfirm, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) override;
    virtual void OnRspQrySettlementInfoConfirm(CThostFtdcSettlementInfoConfirmField *pSettlementInfoConfirm, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) override;
    virtual void OnRspQryTradingAccount(CThostFtdcTradingAccountField *pTradingAccount, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) override;
    virtual void OnRspQryInvestorPosition(CThostFtdcInvestorPositionField *pInvestorPosition, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) override;
    virtual void OnRspQryInstrument(CThostFtdcInstrumentField *pInstrument, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) override;
    virtual void OnRspError(CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) override;
    virtual void OnRtnOrder(CThostFtdcOrderField *pOrder) override;
    virtual void OnRtnTrade(CThostFtdcTradeField *pTrade) override;
    virtual void OnRspQryOrder(CThostFtdcOrderField *pOrder, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) override;
    virtual void OnRspOrderInsert(CThostFtdcInputOrderField *pInputOrder, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) override;
    virtual void OnErrRtnOrderInsert(CThostFtdcInputOrderField *pInputOrder, CThostFtdcRspInfoField *pRspInfo) override;
    virtual void OnErrRtnOrderAction(CThostFtdcOrderActionField *pOrderAction, CThostFtdcRspInfoField *pRspInfo) override;
    virtual void OnRspOrderAction(CThostFtdcInputOrderActionField *pInputOrderAction, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) override;
};

#endif // TRTRADERSPI_H
